package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.RedisConst;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * @ClassName SetmealController
 * @Description: TODO
 * @Author 郑乾
 * @Date 2020-06-24
 * @Version V1.0
 **/
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Reference
    SetmealService setmealService;
    @Autowired
    JedisPool jedisPool;

    @RequestMapping("getToken")
    public Result getToken() {
        log.debug("获取token");
        String accessKey = "dCz2zbkYrrggeNsyBoZ5qECgGoOKQ4K1jZ49oXL4";
        String secretKey = "WsKVSX9jn1pYNEIYgThpgJVNSBARFgmQUvFeLVXX";
        String bucket = "itcast366";
        Auth auth = Auth.create(accessKey, secretKey);
        String uploadToken = auth.uploadToken(bucket);
        log.debug("获取token成功:" + uploadToken);
        return new Result(true, MessageConst.GET_QINIU_TOKEN_SUCCESS, uploadToken);
    }

    @RequestMapping("add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        setmealService.add(setmeal, checkgroupIds);
        log.debug("添加成功");
        return new Result(true, MessageConst.ADD_SETMEAL_SUCCESS);
    }

    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("addImgName")
    public Result addImgName(String imgName) {
        log.debug("图片名称..." + imgName);
        jedisPool.getResource().sadd(RedisConst.SETMEAL_PIC_RESOURCES, imgName);
        log.debug("图片添加到redis成功");
        return new Result(true, MessageConst.ADD_IMGNAME_REDIS);
    }

    @RequestMapping("delById")
    public Result delById(Integer id) {
        setmealService.delById(id);
        return new Result(true, MessageConst.DELETE_SETMEAL_SUCCESS);
    }

    @RequestMapping("findById")
    public Result findById(Integer id) {
        Setmeal setmeal = setmealService.findById(id);
        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, setmeal);
    }

    @RequestMapping("findCheckgroupIdsWithSetmeal")
    public Result findCheckgroupIdsWithSetmeal(Integer id) {
        List<Integer> list = setmealService.findCheckgroupIdsWithSetmeal(id);
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, list);
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody Setmeal setmeal,Integer[]checkgroupIds){
        setmealService.edit(setmeal,checkgroupIds);
        return new Result(true,MessageConst.EDIT_SETMEAL_SUCCESS);
    }
}
